<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class company extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('product/company_model');
			$this->load->library('datatables');			
	}
	function get(){
		echo $this->company_model->get();
	}	
		
	function modal_company()
	{
		$this->load->view('product/company/create_company', $this->data);
	
	}
		
	
	function create_company()
	{
		$this->data['page_title'] = "Create company";
		$meta['page_title'] = "Create company";
		
	//	$this->data['groups']=$this->accgroup_model->get_group();
		/*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}for checking the authentication*/

		//validate form input
		$this->form_validation->set_rules('name', 'company Name', 'required|is_unique[company.name]');
		if ($this->form_validation->run() == true)
		{
			$name=strtoupper($this->input->post('name'));
			$description=strtoupper($this->input->post('description'));
			$additional_data = array(
				'name' => strtoupper($this->input->post('name')),
				'description' => strtoupper($this->input->post('description'))
			);
//			print_r($additional_data);exit;
			
		}
		if ($this->form_validation->run() == true) 
		{
			$this->company_model->insert($name, $description ,$additional_data);
			redirect("product/company/get_all_company");
			//check to see if we are creating the user
			//redirect them back to the admin page
			//$this->session->set_flashdata('message', $this->ion_auth->messages());
			//redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			$this->data['description'] = array(
			'name'  => 'description',
			'id'    => 'description',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('description'),
			);
		
			
		//	print_r($this->data);exit;

			$this->load->view('common/header',$meta);
			$this->load->view('product/company/create_company', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function get_all_company(){
		//list the users
		
	$this->data['users'] = $this->company_model->get_company()->result();
			
//			print_r($this->data['users']);exit; 
	// $this->data['user_type'] =  $this->ion_auth_model->get_user_type($this->session->userdata('user_id'))->row();
	  //echo $get_user_type->name;
	   
	  // exit;
	   $meta['page_title'] = "company List";
	   $this->data['message'] = "company Added Successfully";
	 //  $this->session->set_flashdata('message', 'User Created Successfully');
	   $this->load->view('common/header', $meta);
	   $this->load->view('product/company/view_company');
       $this->load->view('common/footer');
	   
		}
	

	function edit_company($id)
	{
		
		$this->data['page_title'] = "Edit company";
		$meta['page_title'] = "Edit company";
		//$this->data['groups']=$this->company_model->get_company();
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}

		$user = $this->company_model->edit_company($id)->row();
		//print_r($user);exit;
		//$groups=$this->ion_auth->groups()->result_array();
		//$currentGroups = $this->ion_auth->get_users_groups($id)->result();
		
		//getting the user type ex. team lead 
		//$get_user_type = $this->ion_auth_model->user_type()->result_array();
		 //$currentUser = $this->ion_auth_model->get_user_type($id)->result();
		
		//getting the user type ex. fedonic 
		//$get_departments = $this->ion_auth_model->departments()->result_array();
		 //$currentDepartments = $this->ion_auth_model->get_user_department($id)->result();
		
		//process the phone number
		//if (isset($user->phone) && !empty($user->phone))
	//	{
			//$user->phone = explode('-', $user->phone);
		//}

		//validate form input
		$this->form_validation->set_rules('name', 'company Name', 'required|xss_clean');
		/*$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
		$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');*/
		
		//$this->form_validation->set_rules('groups', 'Groups', 'xss_clean');

		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/

			$data = array(
				'name' => strtoupper($this->input->post('name')),
				'description' => strtoupper($this->input->post('description'))
			);
			//print_r($data);
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if ($this->form_validation->run() === TRUE)
			{
				$this->company_model->update($user->id, $data);
				redirect("product/company/get_all_company");
				//check to see if we are creating the user
				//redirect them back to the admin page
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		//pass the user to the view
		$this->data['user'] = $user;
		//$this->data['groups'] = $groups;
		//$this->data['currentGroups'] = $currentGroups;
		
		//user type 
		
		//$this->data['user_type'] = $get_user_type;
	//	$this->data['currentUser'] = $currentUser;
		
		//user departments
		
		//$this->data['get_departments'] = $get_departments;
		//$this->data['currentDepartments'] = $currentDepartments;		
		

		$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
		);
		$this->data['description'] = array(
			'name'  => 'description',
			'id'    => 'description',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('description', $user->description),
		);
		
		/*$this->data['phone2'] = array(
			'name'  => 'phone2',
			'id'    => 'phone2',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
		);
		$this->data['phone3'] = array(
			'name'  => 'phone3',
			'id'    => 'phone3',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
		);*/
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Update',
			'class' =>'btn btn-info'
		);
		
		//print_r($this->data);exit;
		$this->data['page_title'] = "Edit company";
		 $this->load->view('common/header',$meta);
		$this->load->view('product/company/edit_company', $this->data, $id);
		 $this->load->view('common/footer');
	}

	

	function delete_company($id){
		$this->company_model->delete($id);
		redirect("product/company/get_all_company");		
	}

public function import()
{ 
$config['upload_path'] = './upload/company/';
		$config['allowed_types'] = 'xls|xls';
		$config['max_size']	= '20480';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
       $userfile = $_FILES['sel_file']['name'];
	  $tmpName  = $_FILES['sel_file']['tmp_name'];
	 
      $data =array( 'upload_data' => move_uploaded_file($tmpName,"./upload/company/$userfile"));	
	
		$filename=$userfile;
		
if (($handle = fopen(base_url()."upload/company/$filename", "r")) !== FALSE) {
	    	while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
    	    	$num = count($data);
      			// 	echo "<p> $num fields in line $row: <br /></p>\n";
       		 	//$row++;
        		$new_data = array();
				for ($c=0; $c < $num; $c++) {
       		 	  $new_data[] =$data[$c];
        		}
    		$final_data[] = $new_data;
			}
		
		    foreach($final_data as $key=>$fd){
 	    	if($key!=0){
			    $name=$fd[0];
				$data['name'] = $fd[0];
				$data['description']=$fd[1];
				$this->company_model->insert_product($data,$name);		
				}

}
}
redirect("product/company/get_all_company");
}

}
?>